package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    private final Integer default_address=0;
    @Transactional
    public void add(AddressBook book){
        book.setUserId(BaseContext.getCurrentId());//设置是当前用户的id，同样存在不安全
        book.setIsDefault(default_address);
        addressBookMapper.add(book);

    }
    public List<AddressBook> getAlladresses(){
        return addressBookMapper.getAlladresses(BaseContext.getCurrentId());
    }
    @Transactional
    public AddressBook getdefault(){
        List<AddressBook> list = addressBookMapper.getAlladresses(BaseContext.getCurrentId());
        for(int k=0;k<list.size();k++){
            AddressBook address = list.get(k);
            if(address.getIsDefault() == 1)return address;
        }
        return null;
    }
    @Transactional

    public void setdefault(Integer id) {
        addressBookMapper.setdefault(id);
    }
    public AddressBook getByid(Integer id){
        return addressBookMapper.getByid(id);
    }
    @Transactional
    public void update(AddressBook book){
        addressBookMapper.update(book);
    }
    @Transactional
    public  void delete(Integer id){
        addressBookMapper.delete(id);
    }
}
